package www.study.com;

//搜索插入位置 https://leetcode.cn/problems/search-insert-position/
public class code35 {
    public static void main(String[] args) {

    }

    class Solution {
        public int searchInsert(int[] nums, int target) {
            return lefLeIndex(nums,target);
        }
        public int lefLeIndex(int[] nums,int target){
            int l = 0;
            int r = nums.length - 1;
            int mid = 0;
            int res = -1;
            while(l <= r){
                mid = l + ((r - l) >> 1);
                if(nums[mid] == target){
                    return mid; //找到了，返回目标值下标
                }else if(nums[mid] > target){
                    r = mid - 1;
                }else{
                    res = mid;
                    l = mid + 1;
                }
            }
            return res + 1; //没找到，返回按顺序被插入的位置
        }
    }
}
